package dao;

import model.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description: Admin operate mysql
 * @author: PanKer
 * @create: 2021-11-19 17:39
 */
public class adminDao {

    public static boolean isExist(Connection conn, String user) throws SQLException {
        String sql = "SELECT * FROM admin WHERE UserName=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, user);
        ResultSet rs = ps.executeQuery();
        return rs.next();
    }

    public static Admin login(Connection conn, Admin admin) throws SQLException {

        String sql = "SELECT * FROM admin WHERE UserName=? and PassWord=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, admin.getUsername());
        ps.setString(2, admin.getPassword());
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            Admin resultAdmin = new Admin(
                    rs.getString("UserName"),
                    rs.getString("PassWord"),
                    rs.getString("Email")
            );                    // 不能是null
            return resultAdmin;
        } else return null;
    }

}
